#include <math.h>
#include <opencv2/imgproc/types_c.h>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/features2d.hpp>

#ifndef M_PI
# define M_PI       3.14159265358979323846   // pi
#endif // !M_PI

void test_detector(){
    cv::Mat image, grey;
    image = cv::imread("/home/wd/图片/12.jpg");
    cv::cvtColor(image, grey, CV_BGR2GRAY);

    // cv::SimpleBlobDetector detector;
    std::vector<cv::KeyPoint> keypoints;
    // detector.detect(grey, keypoints, cv::Mat());

    cv::Ptr<cv::SimpleBlobDetector> mBlobDetectorPtr;
    mBlobDetectorPtr = cv::SimpleBlobDetector::create();
    mBlobDetectorPtr->detect(grey, keypoints, cv::Mat());

    cv::imshow(" ", grey);
    cv::waitKey(0);

}

double angle2radian(double degree, double minDegree, double masDegree) {
    // degree = 360 - degree;
    double flag = (degree < 0) ? -1.0 : 1.0;  //判断正负
    if (degree < 0) {
        degree = degree * (-1.0);
    }
    double angle = degree + minDegree / 60 + masDegree / 3600;
    double result = flag * (angle * M_PI) / 180;
    return result;
}

void vRotationTransform(int dX, int dY, int dAngle, int& dbNewX, int& dbNewY) {
    double degree = angle2radian(dAngle, 0, 0);
    dbNewX = dX * std::cos(degree) - dY * std::sin(degree);
    dbNewY = dX * std::sin(degree) + dY * std::cos(degree);
}

void test_rotate() {
    cv::Mat image, grey;
    image = cv::imread("/home/wd/下载/zbar-0.10/examples/barcode.png");
    cv::cvtColor(image, grey, CV_BGR2GRAY);
    int width = grey.cols;
    int height = grey.rows;
    // cv::imshow("Demp", grey);
    // cv::waitKey(0);
    unsigned char* srcPtr = grey.data;
    unsigned char* destPtr = new unsigned char[width * height];

    try {
        /* code */
        std::cout << "=============start==============" << std::endl;
        for (int piexlIndex = 0; piexlIndex < width * height; piexlIndex++) {
            // getting the x and y coordinates
            auto x = piexlIndex % width;
            auto y = piexlIndex / width;

            // move center to middle of image
            auto srcx = x - width / 2;
            auto srcy = y - height / 2;

            // rotate
            auto newX = 0;
            auto newY = 0;
            vRotationTransform(srcx, srcy, 90, newX, newY);

            newX = newX + width / 2;
            newY = newY + height / 2;

            // if (newX < 0 || newY < 0) {
            //     continue;
            // }

            auto newIndex = newY * width + newX;
            if (newIndex < width * height && newIndex >= 0) {
                destPtr[piexlIndex] = srcPtr[newIndex];
            } else{
                destPtr[piexlIndex] = 0;
            }
        }

        std::cout << "====================" << std::endl;

        cv::Mat resultMat = cv::Mat(height, width, CV_8UC1, (void*)destPtr);
        cv::imwrite("lena.jpg", resultMat);
        cv::imshow("Test", resultMat);
        cv::waitKey(0);

    } catch (const std::exception& e) {
        std::cerr << e.what() << '\n';
    }

    // delete destPtr;
}